// Copyright (c)  YISH. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using NSQL.Sharpen;

namespace NSQL.Expressions.Functions
{
    public class CoalesceFunction : BaseExpression, ICoalesceFunction
    {
        public ExpressionCollection Items { get; }

        public CoalesceFunction(params IExpression[] items)
        {
            if (items.Length < 2)
            {
                throw new ArgumentException("The Coalesce() function items must at least 2.");
            }
            Items = new ExpressionCollection(items);
        }
        public CoalesceFunction(IEnumerable<IExpression> items)
        {
            Items = new ExpressionCollection(items);
            if (Items.Count < 2)
            {
                throw new ArgumentException("The Coalesce() function items must at least 2.");
            }
        }

        public CoalesceFunction(ExpressionCollection expressions)
        {
            Items = expressions ?? throw new ArgumentNullException(nameof(expressions));
        }
        public override T Accept<T>(IExpressionVisitor<T> visitor) => visitor.Visit(this);

        public override void Accept(IExpressionVisitor visitor)=> visitor.Visit(this);

        public override bool IsValid => Items?.Count >= 2;
        protected override IExpression DeepCopy()
        {
            return new CoalesceFunction(Items.DeepClone())
            {
                Tag = Tag
            };
        }

    }
}